package cn.io0288.blog2.db;

import cn.io0288.blog2.model.Article;
import cn.io0288.blog2.utils.JdbcUtils;
import com.alibaba.fastjson2.JSONObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Articles {
    public static ArrayList<Article> getArticles() {
        Connection connection;
        PreparedStatement preparedStatement;
        ResultSet resultSet;

        ArrayList<Article> articles = new ArrayList<>();

        connection = JdbcUtils.getConnection();
        String sql = "SELECT * FROM articles ORDER BY `date`";

        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Article a = new Article(resultSet);
                articles.add(a);
            }
            System.out.println("articles count: " + articles.size());
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return articles;
    }

    public static Article getArticle(int aid) {
        for (Article a : getArticles())
            if (a.aid == aid)
                return a;
        return null;
    }

    public static void setArticle(Article b) {
        Connection connection;
        PreparedStatement preparedStatement;

        connection = JdbcUtils.getConnection();

        String sql = "UPDATE articles SET aid = %d, uid = %d, cid = %d, title = '%s', title2 = '%s', img = '%s', img_alt = '%s', content = %s, lastdate = %s WHERE aid = %d"
                .formatted(b.aid, b.uid, b.cid, b.title, b.title2, b.img, b.img_alt, JSONObject.toJSONString(b.content), b.lastdate, b.aid);
        System.out.println(sql);

        try {
            preparedStatement = connection.prepareStatement(sql);
            int resultSet = preparedStatement.executeUpdate();

            if (resultSet != 0)
                System.out.println("更新成功, OK");

            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void addArticle(Article b) {
        Connection connection;
        PreparedStatement preparedStatement;

        connection = JdbcUtils.getConnection();

        String sql = "INSERT INTO articles VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', %s, '%s', '%s')"
                .formatted(b.aid, b.uid, b.cid, b.title, b.title2, b.img, b.img_alt, JSONObject.toJSONString(b.content), b.lastdate, b.lastdate);
        System.out.println(sql);

        try {
            preparedStatement = connection.prepareStatement(sql);
            int resultSet = preparedStatement.executeUpdate();

            if (resultSet != 0)
                System.out.println("插入成功, OK");

            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}





